﻿using JunQuanPrinter.DB;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace JunQuanPrinter
{
    public partial class frmQry : Form
    {
        private static frmQry m_frm = null;
        private String m_filter = "";
        public static frmQry getClass()
        {
            
            if(m_frm == null)
            {
                m_frm = new frmQry();
            }
            return m_frm;
        }


        public frmQry()
        {
            InitializeComponent();
        }


        /// <summary>
        /// 显示数据
        /// </summary>
        /// <returns></returns>
        private Boolean ShowData()
        {
            try
            {
                m_filter = "";
                if (!edtProNo.Text.Trim().Equals(""))
                {
                    m_filter = String.Format(" and PRODUCT_CODE =  '{0}'", edtProNo.Text.Trim());
                }

                if (!edtLineNum.Text.Trim().Equals(""))
                {
                    m_filter += String.Format(" and LINE_NUM = '{0}'", edtLineNum.Text.Trim());
                }

                DataTable dt = CDataParam.getClass().getData(m_filter);
                dataGridView_Data.DataSource = dt;
                return true;
            }
            catch(Exception ex)
            {
                CTool.ShowMsgErr(ex.Message);
                return false;
            }

        }

        private Boolean DelData()
        {
            try
            {
                if(dataGridView_Data.Rows.Count == 0)
                {
                    return false;
                }

                if (CTool.ShowMsgQ("真的要删除当前查询出来的记录吗？") == DialogResult.No)
                    return false;
                CDataParam.getClass().DelData(m_filter);
                return true;
            }
            catch(Exception ex)
            {
                CTool.ShowMsgErr(ex.Message);
                return false;
            }
        }

        private void frmQry_FormClosed(object sender, FormClosedEventArgs e)
        {
            m_frm = null;
        }

        private void btnQry_Click(object sender, EventArgs e)
        {
            ShowData();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if(DelData())
            {
                ShowData();
                CTool.ShowMsg("删除记录成功！");
            }
        }
    }
}
